//---------------------------------------------------------------------------

#include "vcl_stl.h"
#pragma hdrstop

#include "h_base.h"
#include "h_xml.h"
#include "h_global.h"
using namespace h_base;
using namespace h_xml;

#include "h_g.h"
#include "h_stageicon.h"

//---------------------------------------------------------------------------

__fastcall HStageIcon::HStageIcon(Classes::TComponent* AOwner)
   : TGraphicControl(AOwner)
{
   FCaption = "";
   FType = 0;
   FColor = h_g::itNormal;
   FPartFlag = false;
   FExternFlag = false;
   FEnlarge = 0;
   FBreakpointFlag = false;
}

void __fastcall HStageIcon::DrawDigit(int x, int y, int Digit)
{
   static unsigned char BitmapArray[] =
   {
      0x77, 0x24, 0x5d, 0x6d, 0x2e,
      0x6b, 0x7b, 0x25, 0x7f, 0x6f
   };

   unsigned char Bitmap = BitmapArray[Digit];

   if ( Bitmap & 0x01 )
   {
      Canvas->MoveTo(x, y);
      Canvas->LineTo(x + 11, y);
      Canvas->MoveTo(x + 1, y + 1);
      Canvas->LineTo(x + 10, y + 1);
      Canvas->MoveTo(x + 2, y + 2);
      Canvas->LineTo(x + 9, y + 2);
   }

   if ( Bitmap & 0x02 )
   {
      Canvas->MoveTo(x, y + 2);
      Canvas->LineTo(x, y + 9);
      Canvas->MoveTo(x + 1, y + 3);
      Canvas->LineTo(x + 1, y + 10);
      Canvas->MoveTo(x + 2, y + 4);
      Canvas->LineTo(x + 2, y + 9);
   }

   if ( Bitmap & 0x04 )
   {
      Canvas->MoveTo(x + 8, y + 4);
      Canvas->LineTo(x + 8, y + 9);
      Canvas->MoveTo(x + 9, y + 3);
      Canvas->LineTo(x + 9, y + 10);
      Canvas->MoveTo(x + 10, y + 2);
      Canvas->LineTo(x + 10, y + 9);
   }

   if ( Bitmap & 0x08 )
   {
      Canvas->MoveTo(x + 3, y + 9);
      Canvas->LineTo(x + 8, y + 9);
      Canvas->MoveTo(x + 2, y + 10);
      Canvas->LineTo(x + 9, y + 10);
      Canvas->MoveTo(x + 3, y + 11);
      Canvas->LineTo(x + 8, y + 11);
   }

   if ( Bitmap & 0x10 )
   {
      Canvas->MoveTo(x, y + 12);
      Canvas->LineTo(x, y + 19);
      Canvas->MoveTo(x + 1, y + 11);
      Canvas->LineTo(x + 1, y + 18);
      Canvas->MoveTo(x + 2, y + 12);
      Canvas->LineTo(x + 2, y + 17);
   }

   if ( Bitmap & 0x20 )
   {
      Canvas->MoveTo(x + 8, y + 12);
      Canvas->LineTo(x + 8, y + 17);
      Canvas->MoveTo(x + 9, y + 11);
      Canvas->LineTo(x + 9, y + 18);
      Canvas->MoveTo(x + 10, y + 12);
      Canvas->LineTo(x + 10, y + 19);
   }

   if ( Bitmap & 0x40 )
   {
      Canvas->MoveTo(x, y + 20);
      Canvas->LineTo(x + 11, y + 20);
      Canvas->MoveTo(x + 1, y + 19);
      Canvas->LineTo(x + 10, y + 19);
      Canvas->MoveTo(x + 2, y + 18);
      Canvas->LineTo(x + 9, y + 18);
   }
}

void __fastcall HStageIcon::Paint(void)
{
   int StartX;
   int TmpWidth;

   switch( FType )
   {
   case 0 :
      Canvas->Draw(0, 0, h_g::BitmapJobBg1[FColor]);
      TmpWidth = h_g::BitmapJobBg1[FColor]->Width;
      StartX = TmpWidth;
      for ( int i = 0; i < FEnlarge + 1; i++ )
      {
         Canvas->Draw(StartX, 0, h_g::BitmapJobBg2[FColor]);
         StartX += TmpWidth;
      }
      Canvas->Draw(StartX, 0, h_g::BitmapJobBg3[FColor]);
      break;
   case 1 :
      Canvas->Draw(0, 0, h_g::BitmapConnector[FColor]);
      return;
   case 2 :
      Canvas->Draw(0, 0, h_g::BitmapExternJobBg1[FColor]);
      TmpWidth = h_g::BitmapExternJobBg1[FColor]->Width;
      StartX = TmpWidth;
      for ( int i = 0; i < FEnlarge + 1; i++ )
      {
         Canvas->Draw(StartX, 0, h_g::BitmapExternJobBg2[FColor]);
         StartX += TmpWidth;
      }
      Canvas->Draw(StartX, 0, h_g::BitmapExternJobBg3[FColor]);
      break;
   case 3 :
      Canvas->Draw(0, 0, h_g::BitmapFwBg[FColor]);
      break;
   case 4 :
      Canvas->Draw(0, 0, h_g::BitmapFwBg[FColor]);
      break;
   case 5 :
      Canvas->Draw(0, 0, h_g::BitmapGroupBg1[FColor]);
      TmpWidth = h_g::BitmapGroupBg1[FColor]->Width;
      StartX = TmpWidth;
      for ( int i = 0; i < FEnlarge + 1; i++ )
      {
         Canvas->Draw(StartX, 0, h_g::BitmapGroupBg2[FColor]);
         StartX += TmpWidth;
      }
      Canvas->Draw(StartX, 0, h_g::BitmapGroupBg3[FColor]);
      break;
   case 6 :
      Canvas->Draw(0, 0, h_g::BitmapTimeBg[FColor]);
      break;
   case 7 :
      Canvas->Draw(0, 0, h_g::BitmapBatchBg1[FColor]);
      TmpWidth = h_g::BitmapBatchBg1[FColor]->Width;
      StartX = TmpWidth;
      for ( int i = 0; i < FEnlarge + 1; i++ )
      {
         Canvas->Draw(StartX, 0, h_g::BitmapBatchBg2[FColor]);
         StartX += TmpWidth;
      }
      Canvas->Draw(StartX, 0, h_g::BitmapBatchBg3[FColor]);
      break;
   }

   TRect Rect(ClientRect);
   int i, j;
   int x, y;
   UTF8String Text = FCaption;

   if ( FType == 3 )
      Text = "F";
   if ( FType == 4 )
      Text = "W";

   if ( FType == 6 )
   {
      Canvas->Pen->Style = psSolid;
      Canvas->Pen->Color = h_g::IconTextColors[FColor];
      x = Rect.Left + 38;
      y = Rect.Top + 10;
      Canvas->MoveTo(x, y);
      Canvas->LineTo(x + 5, y);
      Canvas->MoveTo(x, y + 1);
      Canvas->LineTo(x + 5, y + 1);
      Canvas->MoveTo(x + 1, y + 2);
      Canvas->LineTo(x + 4, y + 2);
      Canvas->MoveTo(x + 2, y + 3);
      Canvas->LineTo(x + 3, y + 3);
      x = Rect.Left + 38;
      y = Rect.Top + 20;
      Canvas->MoveTo(x, y);
      Canvas->LineTo(x + 5, y);
      Canvas->MoveTo(x, y - 1);
      Canvas->LineTo(x + 5, y - 1);
      Canvas->MoveTo(x + 1, y - 2);
      Canvas->LineTo(x + 4, y - 2);
      Canvas->MoveTo(x + 2, y - 3);
      Canvas->LineTo(x + 3, y - 3);
      const char *s = Text.c_str();
      DrawDigit(Rect.Left + 12, Rect.Top + 5, s[0] - '0');
      DrawDigit(Rect.Left + 25, Rect.Top + 5, s[1] - '0');
      DrawDigit(Rect.Left + 45, Rect.Top + 5, s[3] - '0');
      DrawDigit(Rect.Left + 58, Rect.Top + 5, s[4] - '0');
   }
   else
   {
      Rect.Left += 6;
      Rect.Right -= 1 + 6;
      Rect.Bottom -= 3;
      Canvas->Font->Name = "Arial";
      i = Canvas->TextWidth(Text);
      j = Rect.Right - Rect.Left;
      if ( i < j )
         x = Rect.Left + ( j - i ) / 2;
      else
         x = Rect.Left;
      i = Canvas->TextHeight(Text);
      j = Rect.Bottom - Rect.Top;
      if( i < j )
         y = Rect.Top + ( j - i ) / 2;
      else
         y = Rect.Top;
      Canvas->Brush->Style = bsClear;
      Canvas->Font->Color = h_g::IconTextColors[FColor];
      Canvas->TextRect(Rect, x, y, Text);
   }

   if ( FBreakpointFlag )
   {
      Canvas->Brush->Style = bsSolid;
      Canvas->Brush->Color = h_g::IconTextColors[FColor];
      TRect LoopRect;
      LoopRect.Left = Rect.Left - 1;
      LoopRect.Top = Rect.Top + 3;
      LoopRect.Right = LoopRect.Left + 4;
      LoopRect.Bottom = LoopRect.Top + 9;
      Canvas->FillRect(LoopRect);
      LoopRect.Left += 6;
      LoopRect.Right += 6;
      Canvas->FillRect(LoopRect);
   }
   if ( FPartFlag )
   {
      Canvas->Brush->Style = bsSolid;
      Canvas->Brush->Color = h_g::IconTextColors[FColor];
      TRect PartRect;
      PartRect.Left = Rect.Right - 3;
      PartRect.Top = Rect.Top + 3;
      PartRect.Right = PartRect.Left + 5;
      PartRect.Bottom = PartRect.Top + 5;
      Canvas->FrameRect(PartRect);
   }
   if ( FExternFlag )
   {
      Canvas->Pen->Style = psSolid;
      Canvas->Pen->Color = h_g::IconTextColors[FColor];
      Canvas->MoveTo(Rect.Right - 7, Rect.Bottom - 5);
      Canvas->LineTo(Rect.Right + 3, Rect.Bottom - 5);
      Canvas->MoveTo(Rect.Right - 6, Rect.Bottom - 6);
      Canvas->LineTo(Rect.Right - 6, Rect.Bottom - 3);
      Canvas->MoveTo(Rect.Right - 5, Rect.Bottom - 7);
      Canvas->LineTo(Rect.Right - 5, Rect.Bottom - 2);
      Canvas->Pixels[Rect.Right - 4][Rect.Bottom - 8] = Canvas->Pen->Color;
      Canvas->Pixels[Rect.Right - 4][Rect.Bottom - 2] = Canvas->Pen->Color;
   }
   if ( FLoopFlag )
   {
      Canvas->Pen->Style = psSolid;
      Canvas->Pen->Color = h_g::IconTextColors[FColor];
      Canvas->Brush->Style = bsCross;
      Canvas->Brush->Color = h_g::IconTextColors[FColor];
      TRect LoopRect;
      LoopRect.Left = Rect.Left - 1;
      LoopRect.Top = Rect.Bottom - 8;
      LoopRect.Right = LoopRect.Left + 7;
      LoopRect.Bottom = LoopRect.Top + 7;
      Canvas->Ellipse(LoopRect);
   }
}

void __fastcall HStageIcon::SetType(int Type)
{
   FType = Type;
   Invalidate();
}

void __fastcall HStageIcon::SetColor(h_g::HIconType Color)
{
   FColor = Color;
   Invalidate();
}

void __fastcall HStageIcon::SetEnlarge(int Enlarge)
{
   FEnlarge = Enlarge;
   Invalidate();
}

void __fastcall HStageIcon::HighLight(void)
{
   SetColor(h_g::itSelected);
}

void __fastcall HStageIcon::NormalLight(void)
{
   SetColor(h_g::itNormal);
}

void __fastcall HStageIcon::SetCaption(const UTF8String &Caption)
{
   FCaption = Caption;
   Invalidate();
}

void __fastcall HStageIcon::SetPartFlag(bool PartFlag)
{
   FPartFlag = PartFlag;
   Invalidate();
}

void __fastcall HStageIcon::SetExternFlag(bool ExternFlag)
{
   FExternFlag = ExternFlag;
   Invalidate();
}

void __fastcall HStageIcon::SetLoopFlag(bool LoopFlag)
{
   FLoopFlag = LoopFlag;
   Invalidate();
}

void __fastcall HStageIcon::SetBreakpointFlag(bool BreakpointFlag)
{
   FBreakpointFlag = BreakpointFlag;
   Invalidate();
}

#pragma package(smart_init)
